var app = getApp(), api = getApp().api;

Page({
  data: {
    cat_id: "",
    keyword: "",
    list: [],
    page: 1,
    no_more: !1,
    loading: !1,
    latitude: 0,
    longitude: 0,
    keyword:"搜下试试，更多商家更划算",
  },
  onLoad: function (t) {
    let x = this;
    wx.getLocation({
      success: function (res) {
        // console.log(res);
        x.setData({
          latitude: res.latitude,
          longitude: res.longitude,
        })
        getApp().page.onLoad(x, t), t.cat_id && (x.data.cat_id = t.cat_id), x.loadShopList();
      },
    })

  },
  loadShopList: function (t) {
    var a = this;

    //0331
    var keyword = this.data.keyword;
    if(keyword == "搜下试试，更多商家更划算"){
      keyword = "";
    }

    a.data.no_more ? "function" == typeof t && t() : a.data.loading || (a.setData({
      loading: !0
    }), getApp().request({
      url: getApp().api.mch.shop_list,
      data: {
        // keyword: a.data.keyword,
        keyword: keyword,
        cat_id: a.data.cat_id,
        page: a.data.page
      },
      success: function (t) {
        if (0 == t.code) {
          for (let i of t.data.list) {
            i.distance = parseFloat( a.getDistance(a.data.latitude, a.data.longitude, parseFloat(i.latitude), parseFloat(i.longitude)));
          }
          for (let i = 0; i < t.data.list.length; i++) {
            for (let j = 0; j < t.data.list.length - 1 - i; j++) {
              if (t.data.list[j + 1].distance < t.data.list[j].distance) {
                let temp = t.data.list[j+1];
                t.data.list[j + 1] = t.data.list[j];
                t.data.list[j] = temp;
              }
            }
          }
          if (!t.data.list || !t.data.list.length) return void a.setData({
            no_more: !0,
            cat_list: t.data.cat_list
          });
          a.data.list || (a.data.list = []), a.data.list = a.data.list.concat(t.data.list),
            a.setData({
              list: a.data.list,
              page: a.data.page + 1,
              cat_list: t.data.cat_list
            });
        }
      },
      complete: function () {
        a.setData({
          loading: !1
        }), "function" == typeof t && t();
      }
    }));
  },
  getRad(d) {
    var PI = Math.PI;
    return d * PI / 180.0;
  },
  getDistance(lat1, lng1, lat2, lng2) {
    var radLat1 = this.getRad(lat1);
    var radLat2 = this.getRad(lat2);
    var a = radLat1 - radLat2;
    var b = this.getRad(lng1) - this.getRad(lng2);
    var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
      Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
    s = s * 6378.137; // EARTH_RADIUS;
    s = Math.round(s * 10000) / 10000; //输出为公里

    var distance = s;
    var distance_str = "";
    s=s.toFixed(2);
    return s;
  },
  onReady: function () {
    getApp().page.onReady(this);
  },
  onShow: function () {
    getApp().page.onShow(this);
  },
  onHide: function () {
    getApp().page.onHide(this);
  },
  onUnload: function () {
    getApp().page.onUnload(this);
  },
  onPullDownRefresh: function () { },
  onReachBottom: function () {
    this.loadShopList();
  },
  searchSubmit: function (t) {
    var a = t.detail.value;
    this.setData({
      list: [],
      keyword: a,
      page: 1,
      no_more: !1
    }), this.loadShopList(function () { });
  },
  // showCatList: function() {
  //     this.setData({
  //         show_cat_list: !0
  //     });
  // },
  // hideCatList: function() {
  //     this.setData({
  //         show_cat_list: !1
  //     });
  // }
});